package gps.gui;

import gps.Colors;
import gps.Direction;
import gps.implementationAPI.GPSStateImplementation;
import gps.states.SquareState;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JPanel;

public class BoardPanel extends JPanel {

	
	public static int BOARDSIZE = 600;
	
	public static int TOKENSIZE = BOARDSIZE/3;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GPSStateImplementation currentState;
	private Graphics currentGraphics;
	
	public BoardPanel(){
		super();
		this.currentState = new GPSStateImplementation();
	}

	public void setCurrentState(GPSStateImplementation state){
		this.currentState = state;
		
	}
	
	public void paintComponent(Graphics g){
		super.paintComponents(g);
		
		currentGraphics = g;
		
		for(int i = 0 ; i < 3 ; i++){
			for(int j = 0 ; j < 3 ; j++){
				SquareState ss = currentState.getElementState(i, j);
				drawAt(i, j, ss);
			}
		}
		
	}
	
	
	
	private void drawAt(int i, int j, SquareState ss){
		Point point = getAxisPoint(i, j);
		
		if(ss == null){
			currentGraphics.setColor(Color.CYAN);
			currentGraphics.fillRect(point.x, point.y, TOKENSIZE, TOKENSIZE);
		} else {
			if(ss.isUniform()) {
				Color color = getColor(ss.getFirstColor());
				currentGraphics.setColor(color);
				currentGraphics.fillRect(point.x, point.y, TOKENSIZE, TOKENSIZE);
			} else {
				if(ss.getDirection() == Direction.VERTICAL){
					currentGraphics.setColor(getColor(ss.getFirstColor()));
					currentGraphics.fillRect(point.x, point.y, TOKENSIZE, TOKENSIZE/2);
					
					currentGraphics.setColor(getColor(ss.getSndColor()));
					currentGraphics.fillRect(point.x, point.y + TOKENSIZE/2, TOKENSIZE, TOKENSIZE/2);
				} else {
					currentGraphics.setColor(getColor(ss.getFirstColor()));
					currentGraphics.fillRect(point.x, point.y, TOKENSIZE/2, TOKENSIZE);
					
					currentGraphics.setColor(getColor(ss.getSndColor()));
					currentGraphics.fillRect(point.x + TOKENSIZE/2, point.y, TOKENSIZE/2, TOKENSIZE);
					
				}
				
				
				
			}
		}
		
	}
	
	private Point getAxisPoint(int i, int j){
		return new Point(i*TOKENSIZE, j*TOKENSIZE);
	}

	private Color getColor(Colors color){
		if(color == Colors.BLACK){
			return Color.BLACK;
		}
			return Color.WHITE;
	}
	
}
